package com.syni.mdd.yhd.common.all.controller.protect;

import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckUserIdentity;
import com.syni.mdd.yhd.common.all.annotation.CheckParamsMaps;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.entity.BmsVideo;
import com.syni.mdd.yhd.common.all.entity.bo.user.CheckUserBo;
import com.syni.mdd.yhd.common.all.service.BmsUserVideoLikeService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.yhd.common.all.utils.RequestParamUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className BmsUserVideoLikeController
 * @description 用户与商家点赞
 * Author cfx
 * @DATE 2019/4/8 9:42
 * @VERSION 1.0
 **/

@RestController
@RequestMapping("/userVideoLike")
public class BmsUserVideoLikeController {

    @Autowired
    BmsUserVideoLikeService bmsUserVideoLikeService;

    /**
     * @Description: 点赞/取消点赞
     * @Param: open_id（用户id）
     * @Param: video_id（视频id）
     * @return: Map
     * @Author: cfx
     * @Date: 2019/4/9
     */
    @CheckParams(params = {"video_id"},checkAuth = true)
    //@ControllerLog(operationAction = "点赞/取消点赞")
    @PostMapping("/likeGivingLike")
    public Map<String, Object> likeOrGivingLike(@RequestBody ParamsMap paramsMap){
        Integer videoId = Integer.parseInt(paramsMap.get("video_id").toString());
        return bmsUserVideoLikeService.operationLike(Integer.parseInt(paramsMap.get("userIdentify").toString()), videoId);
    }

    /**
     * @Description: 获取用户点赞列表（最近9条）
     * @Param: open_id（用户id）
     * @Param: page（页数）
     * @return: Map
     * @Author: cfx
     * @Date: 2019/4/9
     */
    //@ControllerLog(operationAction = "获取点赞列表")
    @PostMapping("/getLikeList")
    public Map<String, Object> likeList(@RequestBody Map<String,Object> paramsMap){
        String needUserId;
        String myUserId = null;
        if(paramsMap.containsKey("user_id") && StringUtils.checkNullAndEmpty(paramsMap.get("user_id"))){
            needUserId = paramsMap.get("user_id").toString();
            CheckUserBo checkUserBo = RequestParamUtils.checkUserBo();
            if(checkUserBo.getCheckStatus().equals(ResultCode.CODE_NORMAL)){
                myUserId = checkUserBo.getUserId();
            }

        }else {
            CheckUserBo checkUserBo = RequestParamUtils.checkUserBo();
            if(!checkUserBo.getCheckStatus().equals(ResultCode.CODE_NORMAL)){
                return ResultMapHelper.result(ResultCode.USER_TOKEN_INVALID,ResultMsg.MSG_USER_TOKEN_EXPIRE);
            }
            needUserId = checkUserBo.getUserId();
            myUserId = needUserId;
        }
        if(needUserId == null){
            return ResultMapHelper.result(ResultCode.CODE_ERROR_03, ResultMsg.CODE_ERROR_03);
        }
        Integer userId = Integer.parseInt(needUserId);
        String latitude = null;
        String longitude = null;
        Integer pageNum = null;
        Integer pageSize = null;
        if(paramsMap.containsKey("latitude")){
            latitude = paramsMap.get("latitude").toString();
        }
        if(paramsMap.containsKey("longitude")){
            longitude = paramsMap.get("longitude").toString();
        }
        if(paramsMap.containsKey("page_num")){
            pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        }
        if(paramsMap.containsKey("page_size")){
            pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        }
        List<BmsVideo> bmsVideos = bmsUserVideoLikeService.getListToFront(userId,latitude,longitude,pageNum,pageSize,myUserId);
        Map<String,Object> userData = new HashMap<>();
        System.out.println("点赞视频数：" + bmsVideos.size());
        userData.put("likeNum",bmsUserVideoLikeService.findUserTotalLike(userId));
        return ResultMapHelper.success(bmsVideos,userData,"获取成功");
    }
}
